from django.shortcuts import render
from django_redis import get_redis_connection
from rest_framework.generics import GenericAPIView, CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from goods.models import SKU
from orders import serializers
from orders.serializers import OrderCommitSerializer


class OrderPlaceView(GenericAPIView):
    serializer_class = serializers.OrderPlaceSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request):
        # 1,获取redis中需要结算的商品
        user = request.user
        redis_conn = get_redis_connection("cart")
        cart_dict = redis_conn.hgetall("cart_%s" % user.id)
        cart_selected_list = redis_conn.smembers("cart_selected_%s"%user.id)

        # 2,拼接数据
        sku_list = []
        for sku_id  in cart_selected_list:
            sku = SKU.objects.get(id=sku_id)
            sku_list.append(sku)

        # 3,获取序列化器
        # serializer = self.get_serializer(instance=sku_list,many=True)

        # 4,返回响应
        contenxt = {
            # "skus":serializer.data,
            "freight":""
        }
        return Response(contenxt)

class OrderCommitView(CreateAPIView):
    serializer_class = OrderCommitSerializer
    permission_classes = [IsAuthenticated]
